﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace SharpMap.Presentation.Wpf
{

    //  This is baiscaly a poormans PropertyDescriptor.
    //  It's reimplemented since TypeDescriptor is not ready,
    //  and in any case DataTemplate provide much nicer way to present the Style
    public class LayerItemViewModel : INotifyPropertyChanged
    {
        public LayerItemViewModel(SharpMap.Map map)
            :this(map,null)
        {
         
        }

        public LayerItemViewModel(SharpMap.Map map, SharpMap.Layers.ILayer layer)
        {
            Map = map;
            Layer = layer;
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set 
            {
                if (Layer == null && Map != null)
                {
                    Layer = Map.GetLayerByName(LayerName);
                }
                if (Layer != null && Layer.Enabled != IsEnabled)
                {
                    IsEnabled = value; 
                    Layer.Enabled = IsEnabled;
                    RaisePropertyChanged("IsEnabled");
                }
  
            }
        }

        public string LayerName
        {
            get { return _layerName; }
            set { _layerName = value; RaisePropertyChanged("LayerName"); }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set 
            { 
                if (IsSelected != value)
                {
                    IsSelected = value;
                    if (Map != null)
                    {
                        if (IsSelected)
                        {
                            Map.SelectLayer(LayerName);
                        }
                        else
                        {
                            Map.DeselectLayer(LayerName);
                        }
                    }
                    
                    RaisePropertyChanged("IsEnabled");
                }
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs args =
                    new PropertyChangedEventArgs(propertyName);
                handler(this, args);
            }
        }

        #endregion

        SharpMap.Map Map
        {
            get { return _map; }
            set { _map = value; }
        }

        SharpMap.Layers.ILayer Layer
        {
            get { return _layer; }
            set { _layer = value; }
        }

        bool _isEnabled;
        bool _isSelected;
        string _layerName;
        SharpMap.Map _map;
        SharpMap.Layers.ILayer _layer;
    }
}
